       subroutine run_rhf_ri_s(icore, isympopa, isympopb, 
     &                         isymvrta, isymvrtb,
     &                         isymmo, nvmax, nomax, nmo)
c    
      use mod_ri 
      use mod_symm
      use mod_iop
      use mod_orbit
      use mod_size
      use mod_ioff
      implicit none
      integer*8, intent(in) :: nmo, nvmax, nomax, isymmo(nmo, 8)
      integer*8, intent(in) :: isymvrta(nvmax,8), isymvrtb(nvmax,8)
      integer*8, intent(in) :: isympopa(nomax,8), isympopb(nomax,8)
      real*4, intent(in) :: icore(*)
      double precision dtmp
c
      integer*8 maxcor, iintfp, iflags
c
      double precision eccsd, escf, scalfact, tmp
      double precision, allocatable::ea(:), eb(:)
c
      integer*8 :: ncycle, a, b
c
      integer*8:: nbasis, nsol, nbas2, no2, nv2, nvo, no4, nv2o2
      integer*8:: iread, isox, isoy, isoz, itmp1, itmp2, imoc, norbs
      integer*8:: iaopop(8), irrep, i, j, nsize, idx, ioffa
      integer*8:: irrepab, irrepcd, irrepc, irrepd
c            
      integer*8:: iijkl, iiabj, iabij, iijka
      integer*8:: nv3, nv3o, i0, i1, i2, i3, iend, nvt
      integer*8:: icontl, nabcdab, numab, numcd, naux8t(8)
      integer*8:: maxcor1, istat, irrepmo(nmo), ioffmo(nmo)
      integer*8:: c, d, numc, numd, irrepci, numi, irrepi
      integer*8:: numab1, numab2, numab3, numcd1, numcd2, numcd3
      integer*8:: vstart, nfirst, nread, nleft, idx2, nsize2
      double precision tt1, tt2,rt1, rt2, rt3, rt4, rt5, rt6 

      integer, allocatable :: isymnaux(:,:)
      real*8, allocatable :: fijaar(:,:), fijaai(:,:)
      real*8, allocatable :: fijabr(:,:), fijabi(:,:)
      real*8, allocatable :: fiaaar(:,:), fiaaai(:,:)
      real*8, allocatable :: fiaabr(:,:), fiaabi(:,:)
      real*8, allocatable :: fabaar(:,:), fabaai(:,:)
      real*8, allocatable :: fababr(:,:), fababi(:,:)
      real*8, allocatable :: fijr(:,:), fiji(:,:)
      real*8, allocatable :: fiar(:,:), fiai(:,:)
      real*8, allocatable :: fabr(:,:), fabi(:,:)
      real*8, allocatable :: iaam(:,:,:), ibbm(:,:,:)
      real*4, allocatable :: rivv(:), rivo(:), rioo(:)
      real*4, allocatable :: rivv2(:), rivo2(:), rioo2(:)
      real*4, allocatable :: rivvtmp(:)
      real*8, allocatable :: ea_resort(:), eb_resort(:)
C
      character*12 string2, sfname
      logical :: lsoineom, lscalar, lbdf, lscalsoc
C     
      integer*8 memsize, rec_char, n, k, nvv, nvo, noo
      integer*8 fcstar, nbas, nocca, noccb, nirreptmp
      integer*8, external :: idsymsz, irpdso, isymoffso, idsymsoc
      double precision, external :: dnrm2, nrm2so
!     write(6,*)'begin clmoioso'

      call walltimeso(tt1)
      zorb(1) = 1
      zorb(2:8) = 0
      idx = 0  
      do n = 1, nmo 
         do irrep = 1, nirrep
         do i = 1, pop(irrep,1)+vrt(irrep,1)
            if(isymmo(i, irrep)==n) then 
               idx = idx + 1
               irrepmo(n) = irrep
               ioffmo(n) = i 
            endif
         enddo
         enddo
      enddo
      naux8t(1:8) = 0
      open(44, file='dfpar.data', form='formatted')
      read(44, *) naux, n, n
      if (n/=nmo) then
          write(6,*) 'error!', n, nmo
          stop
      endif
      allocate(isymnaux(naux, 8), STAT = istat)
      do i = 1, nirrep
         read(44, *) naux8t(i)
         naux8(i) = naux8t(i)
      enddo
      close(44)

      idx = 0
      do n = 1, nmo
         do irrep = 1, nirrep
         do i = 1, pop(irrep,1)+vrt(irrep,1)
            if(isymmo(i, irrep)==n) then
               idx = idx + 1
               irrepmo(n) = irrep
               ioffmo(n) = i
            endif
         enddo
         enddo
      enddo

      do j = 1, 8
      do i = 1, naux
         isymnaux(i,j) = i
      enddo
      enddo

      naux8 = naux 
      nvv = idsymsoc(1, naux8, zorb, 1, vrta, vrta, 1) 
      nvo = idsymsoc(1, naux8, zorb, 1, vrta, popa, 1)
      noo = idsymsoc(1, naux8, zorb, 1, popa, popa, 1)
      write(6,*)' test here in run_rhf_ri_s'

      write(6, '(1x, a, f6.2)') 'size of rivv(Gb):',
     &                          dble(nvv*4.d0/(1024*1024*1024))
      write(6, '(1x, a, f6.2)') 'size of rivo(Gb):',
     &                          dble(nvo*4.d0/(1024*1024*1024))
      write(6, '(1x, a, f6.2)') 'size of rioo(Gb):',
     &                          dble(noo*4.d0/(1024*1024*1024))

      i0 = 1
      i1 = i0 + nvv*2
      i2 = i1 + nvo*2
      i3 = i2 + noo*2

      call ri_resort_symm(icore(i0), icore(i1), icore(i2), icore(i3), nmo, isymnaux,
     &                isymvrta, isympopa, nvmax, nomax, naux8t)
      write(6,*)'finished ri_resort'
      allocate(rivv(nvv), rivo(nvo), rioo(noo), STAT = istat)
      call checkalloc(istat, 'rivv ... in run_rhf_ri')

!     call ri_resort_symm(iaam, icore(i0), icore(i1), icore(i2), nmo, isymnaux, 
!    &                    isymvrta, isympopa, nvmax, nomax, naux8t)
      call dtos(icore(i0), rivv, nvv)
      call dtos(icore(i1), rivo, nvo)
      call dtos(icore(i2), rioo, noo)
      if(ldebug) then 
          nsize = idsymsoc(1, naux8t, zorb, 1, vrta, vrta, 1) 
          write(6,*)'norm of rivv', nsize, nrm2so(nsize, rivv, 1)
          nsize = idsymsoc(1, naux8t, zorb, 1, vrta, popa, 1) 
          write(6,*)'norm of rivo', nsize, nrm2so(nsize, rivo, 1)
          nsize = idsymsoc(1, naux8t, zorb, 1, popa, popa, 1) 
          write(6,*)'norm of rioo', nsize, nrm2so(nsize, rioo, 1)
      endif
      allocate(rivv2(nvv), rivo2(nvo), STAT = istat)
      call checkalloc(istat, 'rivv ... in run_rhf_ri')
      call sstgenso(rivv, rivv2, nsize, naux8t, zorb, vrta, vrta, 
     &             icore, 1, '1324') 
      call sstgenso(rivo, rivo2, nsize, naux8t, zorb, vrt, pop,
     &                                  icore, 1, '1324')

      naux8 = naux8t

!     call getri_vvvv(icore, icore(i1), rivv, rivv2, icore(i2),
!    &              isymvrta, nvmax)
!     nsize = idsymsoc(1, vrta, vrta, 0, vrta, vrta, 0) 
!     write(6,*)'norm of IABCDAA', nrm2so(nsize, icore, 1)
!     nsize = idsymsoc(1, vrta, vrta, 1, vrta, vrta, 1) 
!     write(6,*)'norm of IABCDAB', nrm2so(nsize, icore(i1), 1)

      call clmoioso0_s(1, icore, naux8t, zorb, 1, 
     &                   vrt, vrt, 1, 'RIVVS___')
      call clmoioso0_s(1, icore, naux8t, zorb, 1, 
     &                   vrt, pop, 1, 'RIVOS___')
      call clmoioso0_s(1, icore, naux8t, zorb, 1, 
     &                   pop, pop, 1, 'RIOOS___')
      call putallso_s(rivv, 1, 'RIVVS___') 
      call putallso_s(rivo, 1, 'RIVOS___') 
      call putallso_s(rioo, 1, 'RIOOS___')
      open(44, file='naux8', form='formatted')
      write(44,*) naux8t 
      close(44)
      call walltimeso(tt2)
      write(6,*)'cpu_time for initio------', tt2-tt1
      call walltimeso(tt1)
!-------for ABAB
3000  call clmoioso0_s(1, icore, pop(1,1), pop(1,1), 1, 
     &                        pop(1,1), vrt(1,1), 1, 'IJKAABS_')
!     write(6,*)'pop and vrt', pop(:,1)
!     write(6,*)'pop and vrt', vrt(:,1) 
      call clmoioso0_s(1, icore, vrt(1,1), pop(1,2), 1, 
     &                       vrt(1,1), pop(1,2), 1, 'BIAJABAS')
      call clmoioso0_s(1, icore, vrt(1,2), pop(1,1), 1, 
     &                       vrt(1,2), pop(1,1), 1, 'BIAJAAAS')
      call clmoioso0_s(1, icore, vrt(1,1), pop(1,1), 1, 
     &                       vrt(1,2), pop(1,2), 1, 'BIAJAABS')


      call clmoioso0_s(1, icore, vrt(1,1), vrt(1,2), 1, 
     &                      pop(1,1), pop(1,2), 1, 'ABIJABS_')

      call clmoioso0_s(1, icore, pop(1,1), pop(1,2), 1, 
     &                      pop(1,1), pop(1,2), 1, 'IJKLABS_')
      call walltimeso(tt2)
      write(6,*)'cpu_time for small-------', tt2-tt1
      call walltimeso(tt1)
!     call clmoioso0(1, icore, vrt(1,1), vrt(1,2), 1, 
!    &                     vrt(1,1), vrt(1,2), 1, 'ABCDAB__')
!     call clmoioso0(1, icore, vrt(1,1), vrt(1,2), 0, 
!    &                     vrt(1,1), vrt(1,2), 0, 'ABCDAA__')
      call walltimeso(tt2)
      write(6,*)'cpu_time for abcd--------', tt2-tt1
      call walltimeso(tt1)
    
!      write(6,*)'finished clmoioso'
      i0 = 1
      i1 = i0 + nmo**3
      i2 = i1 + nmo**3
!     enddo
!      write(6,*)'finished mopy2mo!', naux8t
!=====for vvvo
      if(ldebug) then 
         write(6,*)'nirrep', nirrep
         nsize = idsymsoc(1, naux8t, zorb, 1, vrta, vrta, 1) 
         write(6,*)'norm of rivv', nsize, nrm2so(nsize, rivv, 1)
         nsize = idsymsoc(1, naux8t, zorb, 1, vrta, popa, 1) 
         write(6,*)'norm of rivo', nsize, nrm2so(nsize, rivo, 1)
      endif
      if(eri_level==0.or.eri_level==1) then 
      call clmoioso0_s(1, icore, vrt(1,1), vrt(1,2), 1, 
     &                     vrt(1,1), pop(1,2), 1, 'ABCIABS_')
      do irrepi = 1, nirrep
         numi = popa(irrepi)
         do i = 1, numi 
            call getpqrvo_ri(icore, i, irrepi, rivv, rivo2, 1)
            call putpqrso_s(icore, i, irrepi, 1, 'ABCIABS_')
         enddo
      enddo
      endif

!     call VmnpqVmnrs_to_Vpqrs(rivv, rivo, 1, 1, naux8t, zorb, 1, 
!    &                vrta, vrta, 1, vrta, popa, 1, icore, 1, 1.d0,0.d0)
!     nsize = idsymsoc(1, vrta, vrta, 1, vrta, popa, 1)
!     call sstgenso(icore, icore(nsize+1), nsize, vrta, vrta,vrta,popa, 
!    &             icore, 1, '1324') 
!     write(6,*)'norm of ABCIAB', nsize, nrm2so(nsize, icore(nsize+1),1)
!     call putallso_s(icore(nsize+1), 1, 'ABCIABS_')
!=====for vvoo
      call VmnpqVmnrs_to_Vpqrs(rivo, rivo, 1, 1, naux8t, zorb, 1, 
     &                vrta, popa, 1, vrta, popa, 1, icore, 1, 1.d0,0.d0)
      nsize = idsymsoc(1, vrta, popa, 1, vrta, popa, 1)
      call sstgenso(icore, icore(nsize+1), nsize, vrta, popa,vrta,popa, 
     &             icore, 1, '1324') 
!      write(6,*)'norm of ABIJAB', nsize, nrm2so(nsize, icore(nsize+1),1)
      call putallso_s(icore(nsize+1), 1, 'ABIJABS_')
!=====for ooov
      call VmnpqVmnrs_to_Vpqrs(rioo, rivo, 1, 1, naux8t, zorb, 1, 
     &                popa, popa, 1, vrta, popa, 1, icore, 1, 1.d0,0.d0)
      nsize = idsymsoc(1, popa, popa, 1, vrta, popa, 1)
      call sstgenso(icore, icore(nsize+1), nsize, popa, popa,vrta,popa, 
     &             icore, 1, '2413') 
!      write(6,*)'norm of IJKAAB', nsize, nrm2so(nsize, icore(nsize+1),1)
      call putallso_s(icore(nsize+1), 1, 'IJKAABS_')
!=====for oooo
      call VmnpqVmnrs_to_Vpqrs(rioo, rioo, 1, 1, naux8t, zorb, 1, 
     &                popa, popa, 1, popa, popa, 1, icore, 1, 1.d0,0.d0)
      nsize = idsymsoc(1, popa, popa, 1, popa, popa, 1)
      call sstgenso(icore, icore(nsize+1), nsize, popa, popa,popa,popa, 
     &             icore, 1, '1324') 
!      write(6,*)'norm of IJKLAB', nsize, nrm2so(nsize, icore(nsize+1),1)
      call putallso_s(icore(nsize+1), 1, 'IJKLABS_')
!=====<IA||BJ>=(ib|aj)-(ab|ij)  ! saved as BIAJ
      nsize = idsymsoc(1, vrta, popa, 1, vrta, popa, 1) 
      i1 = 1 + nsize
      i2 = i1 + nsize
!-----<Ia||Bj>=(BI|Aj)        ! saved as BIaj 
      call VmnpqVmnrs_to_Vpqrs(rivo, rivo, 1, 1, naux8t, zorb, 1, 
     &                vrta, popa, 1, vrta, popa, 1, icore, 1, 1.d0,0.d0)
!     call myicopyso(icore, icore(i1), nsize)
!     call scalso(nsize, -1.d0, icore(i1), 1
!      write(6,*)'norm of BIAJAABB', nsize, nrm2so(nsize, icore,1)
      call putallso_s(icore, 1, 'BIAJAABS')
!-----<Ia||bJ>=-(IJ|ab)        ! saved as BiAj 
      call VmnpqVmnrs_to_Vpqrs(rivv, rioo, 1, 1, naux8t, zorb, 1, 
     &                vrta, vrta, 1, popa, popa, 1, icore(i1), 1, 1.d0,0.d0)
      call sstgenso(icore(i1), icore(i2), nsize, vrta,vrta,popa,popa, 
     &             icore, 1, '2314')
      call scalso(nsize, -1.d0, icore(i2), 1) 
!      write(6,*)'norm of BIAJABAB', nsize, nrm2so(nsize, icore(i2),1)
      call putallso_s(icore(i2), 1, 'BIAJABAS')
      call axpyso(nsize,  1.d0, icore(i2), 1, icore, 1)
!      write(6,*)'norm of BIAJAAAA', nsize, nrm2so(nsize, icore,1)
      call putallso_s(icore, 1, 'BIAJAAAS')
      call walltimeso(tt2)
      write(6,*)'cpu_time for small calc--', tt2-tt1
      call walltimeso(tt1)
!====VVVV
      if(eri_level==0) then 
      call clmoioso0_s(1, icore, vrt(1,1), vrt(1,2), 1, 
     &                     vrt(1,1), vrt(1,2), 1, 'ABCDABS_')
      do irrepi = 1, nirrep
         numi = vrta(irrepi)
         do i = 1, numi 
            call getpqrvv_ri(icore, i, irrepi, rivv, rivv2, 1)
            call putpqrso_s(icore, i, irrepi, 1, 'ABCDABS_')
         enddo
      enddo
      endif
!     nsize = idsymsoc(1, vrta, vrta, 1, vrta, vrta, 1)
!     i1 = 1
!     i2 = i1 + idsymsoc(1, vrta, vrta, 1, vrta, vrta, 1)
!     i3 = i2 + idsymsoc(1, vrta, vrta, 1, vrta, vrta, 1)
!     call clmoioso0(1, icore, vrta, vrta, 0, vrta, vrta, 0, 'ABCDAB1_')
!     call clmoioso0(1, icore, vrta, vrta, 2, vrta, vrta, 2, 'ABCDAB2_')
!     call VmnpqVmnrs_to_Vpqrs(rivv, rivv, 1, 1, naux8t, zorb, 1, 
!    &                vrta, vrta, 1, vrta, vrta, 1, icore(i1), 1, 1.d0,0.d0)
!     nsize = idsymsoc(1,vrta,vrtb,1 ,vrta,vrtb, 1)
!     write(6,*)'norm of ABCD test  ', dnrm2(nsize, icore(i1), 1)
!     call sstgenso(icore(i1), icore(i2), nsize, vrta,vrta,vrta,vrta, 
!    &             icore, 1, '1324')
!     nsize = idsymsoc(1,vrta,vrtb,1 ,vrta,vrtb, 1)
!     write(6,*)'norm of ABCD test  ', dnrm2(nsize, icore(i2), 1)
!     call vvvv_half(icore(i2), 0.5d0)
!     nsize = idsymsoc(1,vrta,vrtb,1 ,vrta,vrtb, 1)
!     write(6,*)'norm of ABCD test  ', dnrm2(nsize, icore(i2), 1)
!     call iassymso(icore(i2), icore(i1), 1, vrta, vrta, vrta, vrta, 5)
!     nsize = idsymsoc(1,vrta,vrtb,0 ,vrta,vrtb, 1)
!     write(6,*)'norm of ABCD test  ', dnrm2(nsize, icore(i1), 1)
!     call icompso(icore(i1), icore(i3), vrta, vrta, 0, 1, 
!    &              vrta, vrta,1, 0, 1)
!     call putallso(icore(i3), 1, 'ABCDAB1_') 
!     nsize = idsymsoc(1,vrta,vrtb,1 ,vrta,vrtb, 1)
!     write(6,*)'norm of ABCD test 5 ', dnrm2(nsize, icore(i2), 1)
!     call isymtrso(icore(i2), icore(i1), vrta, vrta, 1, 1, vrta, vrta,
!    &                     1, 0, 1)
!     nsize = idsymsoc(1, vrta, vrta, 1, vrta, vrta, 1)
!     call axpyso(nsize, 1.d0, icore(i1), 1, icore(i2), 1)
!     nsize = idsymsoc(1,vrta,vrtb,1 ,vrta,vrtb, 1)
!     write(6,*)'norm of ABCD test 5 ', dnrm2(nsize, icore(i2), 1)
!     call icompsovv(icore(i2), icore(i1), 0.5d0)
!     nsize = idsymsoc(1,vrta,vrtb,2 ,vrta,vrtb, 2)
!     write(6,*)'norm of ABCD test 6 ', dnrm2(nsize, icore(i1), 1)
!     call putallso(icore(i1), 1, 'ABCDAB2_') 
!     call icompso(icore(i2), icore(i1), vrta, vrta, 1, 2, 
!    &                       vrta, vrta, 1, 1, 1)
!     call icompso(icore(i1), icore(i2), vrta, vrta, 2, 1, 
!    &                       vrta, vrta, 1, 2, 1)
!     nsize = idsymsoc(1,vrta,vrtb,2 ,vrta,vrtb, 2)
!     write(6,*)'norm of ABCD test 6 ', dnrm2(nsize, icore(i2), 1)
!     call vvvv_half2(icore(i2), 0.5d0) 
!     write(6,*)'norm of ABCD test 6 ', dnrm2(nsize, icore(i2), 1)
!     call putallso(icore(i2), 1, 'ABCDAB2_') 
!     
!     call getri_vvvv_io(rivv, rivv2, icore,
!    &              isymvrta, nvmax)
!     call walltimeso(tt2)
!     write(6,*)'cpu_time for large calc', tt2-tt1
      if(ldebug) then 
         call walltimeso(tt1)
         call getallso_s(icore, 1, 'IJKLABS_')
         nsize = idsymsoc(1, popa, popb, 1, popa, popb, 1)
         write(6,*)'norm of IJKLAB  ',nsize, nrm2so(nsize, icore, 1)
   
         call getallso_s(icore, 1, 'IJKAABS_')
         nsize = idsymsoc(1, popa, popb, 1, popa, vrtb, 1)
         write(6,*)'norm of IJKAAB  ',nsize, nrm2so(nsize, icore, 1)
   
         call getallso_s(icore, 1, 'ABIJABS_')
         nsize = idsymsoc(1, vrta,vrtb,1,popa,popb, 1)
         write(6,*)'norm of ABIJAB  ',nsize, nrm2so(nsize, icore, 1)
   
         call getallso_s(icore, 1, 'BIAJAABS')
         nsize = idsymsoc(1, vrta,popa,1,vrtb,popb, 1)
         write(6,*)'norm of BIAJAABB',nsize, nrm2so(nsize, icore, 1)
   
         call getallso_s(icore, 1, 'BIAJABAS')
         nsize = idsymsoc(1,vrta,popb,1, vrta, popb,1)
         write(6,*)'norm of BIAJABAB',nsize, nrm2so(nsize, icore, 1)
   
         call getallso_s(icore, 1, 'BIAJAAAS')
         nsize = idsymsoc(1,vrtb,popa, 1,vrtb, popa,1)
         write(6,*)'norm of BIAJAAAA',nsize, nrm2so(nsize, icore, 1)
   
      if(eri_level==0.or.eri_level==1) then 
         call getallso_s(icore, 1, 'ABCIABS_')
         nsize = idsymsoc(1,vrta,vrtb,1 ,vrta,popb, 1)
         write(6,*)'norm of ABCIAB  ',nsize, nrm2so(nsize, icore, 1)
      endif
!     call getallso_s(icore, 1, 'ABCDAB1_')
!     nsize = idsymsoc(1,vrta,vrtb,0 ,vrta,vrtb, 0)
!     write(6,*)'norm of ABCDAB1  ', dnrm2(nsize, icore, 1)

!     call getallso_s(icore, 1, 'ABCDAB2_')
!     nsize = idsymsoc(1,vrta,vrtb,2 ,vrta,vrtb, 2)
!     write(6,*)'norm of ABCDAB2  ', dnrm2(nsize, icore, 1)

         call walltimeso(tt2)
         write(6,*)'cpu_time for bench', tt2-tt1
      endif
cc---------
!      write(6,*)'finished cc_inter'
      return
      end



